package javaBase.javaLang.classLoader;

import com.sun.org.apache.bcel.internal.util.ClassLoader;
import javaBase.javaLang.TestClass;

import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.text.DecimalFormat;

/**
 * Created by Administrator on 2017/4/13 0013.
 */
public class TestClassLoader {
    public static void test() {
        System.out.println(TestClassLoader.class.getClassLoader());
        System.out.println(TestClass.class.getClassLoader());

    }

    public void testBootStrapClassLoader() {
        URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
        for (int i = 0; i < urls.length; i++) {
            System.out.println(urls[i].toExternalForm());
        }
    }

    public void testNewworkClassLoader() {
        String className = "javaBase.javaLang.classLoader.NetClassLoaderSimple";
        ClassLoader cl1 = new ClassLoader();
        ClassLoader cl2 = new ClassLoader();
        try {
            Class<?> c1 = cl1.loadClass(className);
            Class<?> c2 = cl2.loadClass(className);
            Object obj1 = c1.newInstance();
            Object obj2 = c2.newInstance();
            c1.getMethod("setNetClassLoaderSimple", Object.class).invoke(obj1, obj2);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void testClassLoader() {
        java.lang.ClassLoader loader = TestClassLoader.class.getClassLoader();
        while (loader != null) {
            System.out.println(loader);
            loader = loader.getParent();
        }
        System.out.println(loader);
    }

    public static void main(String[] args) {
        TestClassLoader tsl = new TestClassLoader();
        //tsl.test();
        //tsl.testBootStrapClassLoader();
        //tsl.testNewworkClassLoader();
        //tsl.testClassLoader();
        String feedback = String.valueOf(new DecimalFormat("#.##").format(Double.parseDouble("2") * 100 / Double.parseDouble("1100")));
        System.out.print(feedback);
    }
}
